#!/usr/bin/perl -w

##HACKABOT_HELP##
# Get the url of a wiktionary article
# !wiktionary some article
##HACKABOT_HELP##

use strict;
use URI::Escape;
use Hackabot::Client;

my $hbc = Hackabot::Client->new;
my $search = $hbc->readline;
my $asker = $hbc->sent_by;

if ($search) {
	$search = uri_escape($search);
	my $google = `lynx --head --dump "http://en.wiktionary.org/wiki/Special:Search?search=$search\&go=Go"`;
	my $url;
	foreach(split(/\n/,$google)) {
		if (/^Location:\s*(.+)/) {
			$url = $1;
		}
	}
	if (defined $url) {
		print "send $asker: Wiktionary: $url\n";
	}
	else {
		print "send $asker: Wiktionary didn't say much :-/\n";
	}
}
